const nodemailer = require('nodemailer')
const {
  MAILER_EMAIL,
  MAILER_PASSWORD,
} = require('../../config')
const template = require('./template.html.js')

const emailParams = {
  subjectTag: process.env.NODE_ENV === 'development' ? '[Dev Paifile]' : '[Paifile]',
  greeting: '你好，',
  mainContent: '',
  hasButton: false,
  buttonTitle: '',
  buttonUrl: '',
  additionalContent: '來自 Paifile 團隊',
  tintColor: '#7773BF',
  backgroundColor: '#afaafb',
  logoImageUrl: 'https://s3-ap-southeast-1.amazonaws.com/paifile.static/paifile-logo/paifile-logo-13.png',
  logoAlt: 'Paifile',
  logoTitle: 'Paifile',
  contactEmail: 'contact@paifile.hk',
  footer: '© Copyright 2018 Paifile Limited. All Rights Reserved.',
}

const mailClient = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: MAILER_EMAIL,
    pass: MAILER_PASSWORD,
  },
})

// const mailOptions = {
//   from: 'kk@bichenkk.com',
//   to: 'kk@bichenkk.com',
//   subject: 'Testing from Binatir',
//   text: 'Welcome to Binatir.'
// }
mailClient.send = mailOptions => (
  new Promise((resolve, reject) => {
    mailClient.sendMail(mailOptions, (error, info) => {
      if (error) {
        return reject(error)
      }
      return resolve(info)
    })
  })
)

mailClient.sendUserResetPasswordEmail = ({ name, email, resetPasswordUrl }) => {
  const subject = `${emailParams.subjectTag} 重設你的密碼`
  const text = `你好 ${name}, 我們收到了重設帳戶 ${email} 密碼的請求。您可以在此處重設你的密碼 ${resetPasswordUrl}。`
  const mailOptions = {
    from: MAILER_EMAIL,
    to: email,
    subject,
    text,
    html: template({
      ...emailParams,
      greeting: `你好 ${name},`,
      mainContent: `我們收到了重設帳戶 ${email} 密碼的請求。您可以在此處重設你的密碼。`,
      hasButton: true,
      buttonTitle: '重設密碼',
      buttonUrl: resetPasswordUrl,
      additionalContent: `如果您忽略此消息，則不會更改您的密碼。 如果您未請求重設密碼，請告知我們。`,
    }),
  }
  return mailClient.send(mailOptions)
}

mailClient.sendMerchantResetPasswordEmail = ({ name, email, resetPasswordUrl }) => {
  const subject = `${emailParams.subjectTag} 重設你的密碼`
  const text = `你好 ${name}, 我們收到了重設帳戶 ${email} 密碼的請求。您可以在此處重設你的密碼 ${resetPasswordUrl}。`
  const mailOptions = {
    from: MAILER_EMAIL,
    to: email,
    subject,
    text,
    html: template({
      ...emailParams,
      greeting: `你好 ${name},`,
      mainContent: `我們收到了重設帳戶 ${email} 密碼的請求。您可以在此處重設你的密碼。`,
      hasButton: true,
      buttonTitle: '重設密碼',
      buttonUrl: resetPasswordUrl,
      additionalContent: `如果您忽略此消息，則不會更改您的密碼。 如果您未請求重設密碼，請告知我們。`,
    }),
  }
  return mailClient.send(mailOptions)
}

mailClient.sendMerchantInvitationEmail = ({
  email,
  merchantName,
  merchantEmail,
  organisationName,
  registerUrl,
}) => {
  const subject = `${emailParams.subjectTag} ${merchantName} 邀請您加入 Paifile Provider Portal`
  const text = `Hello, ${merchantName} (${merchantEmail}) invited you to join ${organisationName} on Paifile Merchant Portal. Please go to ${registerUrl} and register.`
  const mailOptions = {
    from: MAILER_EMAIL,
    to: email,
    subject,
    text,
    html: template({
      ...emailParams,
      greeting: `你好`,
      mainContent: `Hello, ${merchantName} (${merchantEmail}) invited you to join ${organisationName} on Paifile Merchant Portal.`,
      hasButton: true,
      buttonTitle: 'Accept and Register',
      buttonUrl: registerUrl,
      additionalContent: `From Paifile Team`,
    }),
  }
  return mailClient.send(mailOptions)
}

module.exports = mailClient
